﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using SCM.Common;
using SCM.Common.Extensions;
using SCM.Model.DBEntity;
using SCM.Servie.IBusiness;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;

namespace SCM.Manage.Controllers
{
    public abstract class BaseController : Controller
    {
        private IServiceScope scope = null;

        #region 基础数据
        public List<T_Material_Catalog> GetMaterialCatalog()
        {
            string key = "MaterialCatalog";
            var service = GetService<IRedisHelper>();
            if (service.HasKey(key))
            {
                return service.Get<List<T_Material_Catalog>>(key);
            }
            else 
            {
                var mcService = GetService<IMaterialCatalogService>();
                var list = mcService.GetList("");
                service.Set(key, JsonConvert.SerializeObject(list), 3600);
                return list;
            }
        }
        #endregion


        /// <summary>
        /// 获取依赖注入服务
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        protected T GetService<T>()
        {
            if (scope == null)
            {
                scope = ServiceLocator.Instance.CreateScope();
            }
            return scope.ServiceProvider.GetRequiredService<T>();
        }

        /// <summary>
        /// 返回成功
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data"></param>
        /// <returns></returns>
        protected IActionResult GetOk<T>(T data)
        {
            return new JsonResult(new DataPack<T>() { Code = ResultCode.OK, Data = data, Message = "操作成功" });
        }

        /// <summary>
        /// 错误
        /// </summary>
        /// <param name="message"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        protected IActionResult Error(string message, ResultCode code = ResultCode.ParameterError)
        {
            return new JsonResult(new DataPack<string>() { Code = code, Message = message });
        }


        /// <summary>
        /// 登录用户ID
        /// </summary>
        protected string CurrentUserId
        {
            get
            {
                var data = HttpContext.User?.Claims.FirstOrDefault(p => p.Type == ClaimTypes.Sid);
                if (data == null)
                {
                    return "";
                }
                return data.Value;
            }
        }

        /// <summary>
        /// 登录用户名称
        /// </summary>
        protected string CurrentUserName
        {
            get
            {
                return HttpContext.User?.Identity.Name;
            }
        }


        /// <summary>
        /// 登录用户企业ID
        /// </summary>
        protected string CurrentCompanyID
        {
            get
            {
                var data = HttpContext.User?.Claims.FirstOrDefault(p => p.Type == "CompanyID");
                if (data == null)
                {
                    return "";
                }
                return data.Value;
            }
        }

        /// <summary>
        /// 登录用户企业名称
        /// </summary>
        protected string CurrentCompanyName
        {
            get
            {
                var data = HttpContext.User?.Claims.FirstOrDefault(p => p.Type == "CompanyName");
                if (data == null)
                {
                    return "";
                }
                return data.Value;
            }
        }

        /// <summary>
        /// 登录用户企业ID
        /// </summary>
        protected string CurrentRoleID
        {
            get
            {
                var data = HttpContext.User?.Claims.FirstOrDefault(p => p.Type == "RoleID");
                if (data == null)
                {
                    return "";
                }
                return data.Value;
            }
        }

        /// <summary>
        /// 登录用户企业名称
        /// </summary>
        protected string CurrentRoleName
        {
            get
            {
                var data = HttpContext.User?.Claims.FirstOrDefault(p => p.Type == ClaimTypes.Role);
                if (data == null)
                {
                    return "";
                }
                return data.Value;
            }
        }
    }
}
